﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_NameManage : ATPages
{
    ZSql zsql = new ZSql();

    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblwz.Text = f.getModuleidAdd(hfxs.Value);

            gdata();
            initData();

        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddName.aspx?moduid=" + hfxs.Value + "&action=add");
    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string ComoditiesNameId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("AddName.aspx?ComoditiesNameId=" + ComoditiesNameId + "&moduid=" + hfxs.Value + "&action=flag");

    }

    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string ComoditiesNameId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("AddName.aspx?ComoditiesNameId=" + ComoditiesNameId + "&moduid=" + hfxs.Value + "&action=update");

    }

    protected void btnSelec_Click(object sender, EventArgs e)
    {
        string sql = "select c.ComoditiesNameId,c.AuditState, c.ComoditiesName,c.ComoditiesNameOrder,c.UserName,c.usTime,a.ComoditiesType,b.Brand ,c.namebh,(SELECT COUNT(*) FROM CS_Stock  WHERE CS_Stock.ComoditiesName = c.ComoditiesNameId) AS Stock from ComoditiesType as a ,Brand as b,ComoditiesName as c  where a.TypeId=c.TypeId and b.BrandId=c.BrandId";
        if (txtseletype.Text != "")
        {
            sql += " and a.ComoditiesType like'" + Common.FormatParameter(txtseletype.Text) + "%'";
        }

        if (txtselecName.Text != "")
        {
            sql += " and c.namebh like'" + Common.FormatParameter(txtselecName.Text) + "%'";
        }

        if (txtseleband.Text != "")
        {

            sql += " and b.Brand like'" + Common.FormatParameter(txtseleband.Text) + "%'";

        }

        sql += " order by a.TypeId desc";

        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

    }

    protected void btnallflag_Click(object sender, EventArgs e)
    {

        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvDataName.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataName.Rows.Count; i++)
            {
                CheckBox ck = GvDataName.Rows[i].Cells[9].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    int flag = 1;
                    allflag.Open("update ComoditiesName set AuditState=" + flag + " where ComoditiesNameId=" + this.GvDataName.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataName.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvDataName.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataName.Rows.Count; i++)
            {
                CheckBox ck = GvDataName.Rows[i].Cells[9].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete ComoditiesName  where ComoditiesNameId=" + this.GvDataName.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataName.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }
    private void initData()
    {





    }
    private void gdata()
    {
        string sql = "select c.ComoditiesNameId,c.AuditState, c.ComoditiesName,c.ComoditiesNameOrder,c.UserName,c.usTime,a.ComoditiesType,b.Brand,c.namebh,(SELECT COUNT(*) FROM CS_Stock  WHERE CS_Stock.ComoditiesName = c.ComoditiesNameId) AS Stock from ComoditiesType as a ,Brand as b,ComoditiesName as c  where a.TypeId=c.TypeId and b.BrandId=c.BrandId  order by a.TypeId desc";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void GvDataName_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 11, GvDataName, btndel, btnallflag, btnAdd);

        if (e.Row.RowType == DataControlRowType.DataRow)
        {   //审核状态
            string AuditState = DataBinder.Eval(e.Row.DataItem, "AuditState").ToString();
            if (AuditState == "1")
            {
                e.Row.Cells[8].Text = "已通过";
                e.Row.Cells[8].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "0")
            {
                e.Row.Cells[8].Text = "未通过";
                e.Row.Cells[8].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "")
            {
                e.Row.Cells[8].Text = "未审核";
                e.Row.Cells[8].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            //删除是否可用
            string delDisabled = DataBinder.Eval(e.Row.DataItem, "Stock").ToString();
            if (Convert.ToInt32(delDisabled)>0)
            {
                (e.Row.Cells[9].FindControl("chkDelete") as CheckBox).Enabled = false;
            }

        }

    }
}
